from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time


# 创建一个新的Chrome服务实例
# 设置Chrome选项
chrome_options = Options()
chrome_options.add_argument("--headless")  # 运行时不会打开浏览器窗口
chrome_options.add_argument("--disable-gpu")

# 创建一个webdriver实例，指定chromedriver的路径
driver = webdriver.Chrome(executable_path='D:\software\chromedriver133\chromedriver-win64\chromedriver-win64\chromedriver', options=chrome_options)


url = "https://top.baidu.com/board?tab=realtime"
driver.get(url)

# 等待页面加载完成
time.sleep(5)  # 根据实际情况调整等待时间

# 假设我们想要查找的元素有特定的类名
elements = driver.find_elements(By.CLASS_NAME, 'c-single-text-ellipsis')

hotspots = []
for element in elements:
    title = element.text
    if title:
        hotspots.append(title)

# 打印结果
for idx, hotspot in enumerate(hotspots[:100], start=1):
    print(f"{idx}. {hotspot}")

# 关闭webdriver
driver.quit()